package com.hyw.trace.configuration.sampler;

import java.util.Map;
import java.util.function.Function;

import org.springframework.util.Assert;

import com.hyw.trace.configuration.meta.TraceEvent;
import com.hyw.trace.configuration.sample.annotation.TraceSource;

/**
 * 一个抽象的采集模板类
 * 
 * @author hongyu
 */
public abstract class AbstractSamplerTemplate {
  /**
   * 映射采集率的最小值，inclusive
   */
  protected static final int MIN_BOUND = 0;

  /**
   * 映射采集率的最大值，exclusive
   */
  protected static final int MAX_BOUND = 100;

  public abstract void run(Object[] args, TraceSource annotation);

  public abstract void run(Object[] args, TraceSource annotation, Function<Map<String, ?>, TraceEvent> mapper);

  /**
   * 添加 一个机制,用于对采集点进行过滤
   * 
   * @param annotation
   * @return 为false时,该采集点不进行采集
   */
  protected boolean isSupport(TraceSource annotation) {
    boolean validRatio = annotation.ratio() >= 0 && annotation.ratio() <= 100;
    Assert.isTrue(validRatio, "采集率的取值范围为【0,100】");
    return true;
  }

}
